package com.fenghongzhang.module_main.ui.post

import android.view.View
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.fenghongzhang.lib_core.utils.ToastUtils
import com.fenghongzhang.lib_core.utils.getAliOSSVideoImage
import com.fenghongzhang.lib_core.utils.setPortrait
import com.fenghongzhang.lib_core.utils.setLandscape
import com.fenghongzhang.lib_net.model.Image


import com.fenghongzhang.module_main.databinding.LayoutPostItemBinding
import com.fenghongzhang.module_main.glide.glide.GlideApp
import com.fenghongzhang.module_main.glide.pic.Info
import com.google.android.exoplayer2.util.MimeTypes
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


class PostViewHolder(
    itemView: View,
    private val onUserClick: ((PostModel) -> Unit)? = null,
    private val onImageClick: ((Int, Int, List<Image>, List<Info>) -> Unit)? = null,
    )

    : RecyclerView.ViewHolder(itemView) {

    private val mBinding: LayoutPostItemBinding by lazy {
        DataBindingUtil.getBinding(this.itemView)!!
    }
    var postModel: PostModel? = null

    var isClickSpan: Boolean = false
    fun setMaxLines(max: Int) {
        mBinding.tvPostContent.maxLines = max
    }

    fun initView(post: PostModel, position: Int) {

        mBinding.apply {
           handlePicOrVideo(post, position)
            postModel = post
            //处理帖子中携带的话题
            if (!post.topics.isNullOrEmpty()){
                //   handlePostTopic(post, post.topics)
            }
            else {
                tvPostContent.text = post.requireContent()
            }
        }
    }

    private fun LayoutPostItemBinding.handlePicOrVideo(post: PostModel, position: Int) {
        post.img ?: return
        if (post.img.size == 1 && MimeTypes.isVideo(post.img[0].contentType)) {
            lVideo.setOnClickListener {
                // VideoPlayerActivity.jump(post.img[0].url)
                clickItem()
            }
            //设置视频
            val video = post.img.firstOrNull() ?: return
            val videoHeight = video.imageHeight
            val videoWidth = video.imageWidth
            if (videoHeight > videoWidth) {
                lVideo.setPortrait()
                GlideApp.with(this.root).load(getAliOSSVideoImage(video.url)).applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 343, 180)
                    .into(ivVideo)

            } else {
                lVideo.setLandscape()
                GlideApp.with(this.root).load(getAliOSSVideoImage(video.url)).applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 311, 233)
                    .into(ivVideo)
            }
            ivPlay.visibility = View.VISIBLE
            //视频
            nineGridView.visibility = View.GONE
            lVideo.visibility = View.VISIBLE
        } else {
            //图片
            nineGridView.setPicsList(post.img)
            onImageClick?.let {
                nineGridView.setOnItemImageClickListener(onImageClick)
            }
            if(onImageClick == null){
                ToastUtils.showShort(itemView.context,"请设置图片点击事件")
            }
            nineGridView.initView(position, ::clickItem)
            nineGridView.visibility = View.VISIBLE
            lVideo.visibility = View.GONE
        }

    }

    fun clickUser() {
        if (mBinding.post!!.isOfficial()) return
        onUserClick?.invoke(mBinding.post!!)
    }
    fun clickItem() {
        if (isClickSpan) {
            isClickSpan = false
            return
        }
       // onItemClick?.invoke(mBinding.post!!, this.absoluteAdapterPosition)
    }


}